package com.lingxin.gps.wap;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.StrKit;
import com.pisces.core.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 登录验证
 */
public class LoginInterceptor implements Interceptor {

	private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
	
	@Override
	public void intercept(Invocation inv) {
		BaseController context = (BaseController) inv.getController();
		try {
			logger.info("登录验证{}", "LoginInterceptor");
			// 用户ID
			Long userId = context.getCookieToLong("uid");
			String token = context.getCookie("token");

			if (userId != null && StrKit.notBlank(token)) {
				context.setAttr("uid", userId);
				context.setAttr("token", token);
			}
			inv.invoke();

		} catch (Exception e) {
			e.printStackTrace();

			String loginUrl = AppConfig.APP_DOMAIN + "/user/login";
			context.redirect(loginUrl, false);
		}

	}
	
}
